﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpDisposeEx
{
    class Program
    {
        static void Main(string[] args)
        {

            string msg = SpDisposeHelper();

            bool hasToolPath = false;
            string toolPath = String.Empty;
            bool hasAsmPath = false;
            string asmPath = String.Empty;
            bool showInXml = false;
            bool raise = false;
          
            try
            {
                for (int i = 0; i < args.Count() ;i++ )
                {
                    string s = args[i];
                    switch (s)
                    {
                        case "-xml":
                            showInXml = true;                           
                            break;
                        case "-raise":
                            raise = true;
                            break;
                        case "-t":
                            hasToolPath = true;
                            toolPath = args[i+1];
                            break;
                        case "-asm":
                            hasAsmPath = true;
                            asmPath = args[i+1];                           
                            break;

                    }
                    
                }
            }
            catch 
            {
                Console.WriteLine("Bad Syntax !");
                Console.WriteLine(msg);
                return;
            }
            string finalMsg = String.Empty;
            if (hasToolPath && (!String.IsNullOrEmpty(toolPath)) && hasAsmPath && !String.IsNullOrEmpty(asmPath))
            {
                SPDisposerWatcher sw = new SPDisposerWatcher();
                finalMsg = sw.ValidateDisposes(toolPath, asmPath, showInXml);
              
            }
            else
            {
                finalMsg = "Bad Syntax !";
              
            }
            if (!String.IsNullOrEmpty(finalMsg))
            {
                Console.WriteLine(finalMsg);
                if (raise) throw new Exception("SpDispose Objects check with errors!");
            }
         
            
        }


        static string SpDisposeHelper()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(new String('#',80));
            sb.Append("SpDisposerChecker, helper utility for Roger Lamb SpDispose Check!\n\r");
            sb.Append("\n\r");
            sb.Append(" Rodrigo Pinto, GISP Sharepoint Solution Architect 2009 \n\r email:stationsolutions@gmail.com \n\r msn:workstationpt@hotmail.com\n\r\n\r");
            sb.Append(" Comunidade Portuguesa SharePoint /Portuguese SharePoint Community\n\r\n\r");
            sb.Append(new String('#', 80));
            sb.Append("\n\r");
            sb.Append(" Use the following sintax:\n\r \n\r");
            sb.Append(" SpDisposerChecker :\n\r\n\r\t-t \n\r\t\t<full file path for Roger Lamb's Tool>\n\r");
            sb.Append("\n\r\t-asm\n\r\t\t<full file path for assemblies>\n\r");
            sb.Append("\n\r\t-xml\n\r\t\t Display it in xml\n\r");
            sb.Append("\n\r\t-raise\n\r\t\t Raise error . Usefull for PostBuildEvents\n\r\n\r");
            sb.Append(new String('#', 80));
            
            sb.Append("\n\r");

            return sb.ToString();

        }
    }
}
